package b1.scenes.suspect
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.home.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;

	public class Suspect extends Sprite
	{
		private var criminalIndex:int;
		private var currentSuspectIndex:int;
		private var battleCase:BattleCaseInfo;
		private var arrestButton:BasicButton;
		private var letGoButton:BasicButton;
		private var suspectStage:SuspectStage;
		private var suspectTotalNum:int;
		private var suspectLeftNumTxt:TextField;
		private var detectiveAnimalId:int;
//		private const buttonWidth:int = 100;
		private const smallFontSize:int = 20;
		private var arrestButtonTip:BasicMessage;
		private var letGoButtonTip:BasicMessage;
		private var titleText1:TextField;
		private var titleText2:TextField;
		private var spiderTip:BasicMessage;
		
		private var suspectBackground:Sprite;
		
		//type, -1,正常探案； 其他值表示特殊案件的index
		public function Suspect(battleCase:BattleCaseInfo, detectiveAnimalId:int, caseName:String)
		{
			super();
			
			this.battleCase = battleCase; 
			this.detectiveAnimalId = detectiveAnimalId;
			//背景
			var SuspectBackground:Class = ResourceHelper.getInstance().getClassByName("SuspectBackground");
//			var suspectBackground:Sprite = new SuspectBackground();
			suspectBackground = new SuspectBackground();
			suspectBackground.width = BFont.stageWidth;
			suspectBackground.height = BFont.stageHeight; 
			addChild(suspectBackground);
			//柜子
			var Cabinet:Class = ResourceHelper.getInstance().getClassByName("Cabinet");
			var cabinet:Sprite = new Cabinet();
			cabinet.height = 400;
			cabinet.scaleX = cabinet.scaleY;
			cabinet.x = 80;
			cabinet.y = 140;
			addChild(cabinet);
			
			//蜘蛛
			var Spider:Class = ResourceHelper.getInstance().getClassByName("SpiderAndNet");
			var spider:BasicMc = new BasicMc(Spider, 1);
			spider.height = 110;
			spider.scaleX = spider.scaleY;
			spider.x = 140;
			spider.addEventListener(MouseEvent.MOUSE_OVER, spiderOnMouseOver);
			spider.addEventListener(MouseEvent.MOUSE_OUT, spiderOnMouseOut);
			//窗户
			var Window:Class = ResourceHelper.getInstance().getClassByName("Window");
			var window:Sprite = new Window();
			window.height = 140;
			window.scaleX = cabinet.scaleY;
			window.x = 590;
			window.y = 56;
			addChild(window);
			//线索标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000);
			titleText.text = caseName + "的线索";
			titleText.x = cabinet.x + (cabinet.width - titleText.width) / 2;
			titleText.y = cabinet.y - titleText.height - 2;
			addChild(titleText);
			
			//用于显示嫌疑犯标题（主人,名字, 第几个嫌疑犯）
			titleText1 = new TextField();
			titleText1.selectable = false;
        	titleText1.autoSize = TextFieldAutoSize.LEFT;
			titleText1.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000);
			addChild(titleText1);
			
			titleText2 = new TextField();
			titleText2.selectable = false;
        	titleText2.autoSize = TextFieldAutoSize.LEFT;
			addChild(titleText2);
			
			//线索
			for (var i:int = 0; i < battleCase.clues.length; i++)
			{
				var clueItem:ClueItem = new ClueItem(battleCase.clues[i]);
				clueItem.x = 103;
				clueItem.y = i * 113 + 180;
				addChild(clueItem);
			}
			//按钮
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			arrestButton = new BasicButton(ButtonBgr1, "指认", smallFontSize);
			arrestButton.x = BFont.stageWidth - arrestButton.width - 60;
			arrestButton.y = 270;
			arrestButton.addEventListener(MouseEvent.CLICK, onArrestButtonClick);
			addChild(arrestButton);
			arrestButton.addEventListener(MouseEvent.MOUSE_OVER, arrestButtonOnMouseOver);
			arrestButton.addEventListener(MouseEvent.MOUSE_OUT, arrestButtonOnMouseOut);
			
			letGoButton = new BasicButton(ButtonBgr1, "放走", smallFontSize);
			letGoButton.x = arrestButton.x;
			letGoButton.y = arrestButton.y + 75;
			letGoButton.addEventListener(MouseEvent.CLICK, onLetGoButtonClick);
			addChild(letGoButton);
			letGoButton.addEventListener(MouseEvent.MOUSE_OVER, letGoButtonOnMouseOver);
			letGoButton.addEventListener(MouseEvent.MOUSE_OUT, letGoButtonOnMouseOut);
			//随机化真实罪犯在嫌疑犯列表中的位置
			var detectiveAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[detectiveAnimalId];
			var levelConfig:LevelConfig = Config.levelConfigs[detectiveAnimal.level - 1];
			suspectTotalNum = 3;//levelConfig.suspectNum;
			criminalIndex = Math.random() * suspectTotalNum;
			currentSuspectIndex = 0;
			//剩余嫌疑犯数量文字
			suspectLeftNumTxt = new TextField();
			suspectLeftNumTxt.selectable = false;
        	suspectLeftNumTxt.autoSize = TextFieldAutoSize.LEFT;
			suspectLeftNumTxt.defaultTextFormat = new TextFormat("微软雅黑", smallFontSize, 0x000000);
			suspectLeftNumTxt.text = "还有 " + suspectTotalNum + " 个";
			suspectLeftNumTxt.x = arrestButton.x;
			suspectLeftNumTxt.y = letGoButton.y + 50;
			addChild(suspectLeftNumTxt);
			
			getNextSuspect();
			addChild(spider);
		}
		
		private function onArrestButtonClick(event:MouseEvent):void
		{
			var claimBones:int = this.battleCase.caseBones * Config.claimRatioForWrongIdentification / 100;
			var detectiveAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[detectiveAnimalId];
			ServerHelper.getInstance().identifySuspectAnimal(this.battleCase.caseId, detectiveAnimal.ownerId, this.suspectStage.animal.id, claimBones, onArrestComplete);
			
		}
		
		private function onArrestComplete(result:String):void
		{
			//搜捕该动物的原籍 
			var homeType:int;
			if (result == "right")
			{
				homeType = 1;
			}
			else
			{
				homeType = 2;
			}
			var detectiveAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[detectiveAnimalId];
			var home:Home = new Home(suspectStage.animal.ownerId, homeType, detectiveAnimal, suspectStage.animal, battleCase);
			SceneContainer.getInstance().loadScene(home);
		}
		
		private function onLetGoButtonClick(event:MouseEvent):void
		{
			getNextSuspect();
			disableLetGoButton();
		}
		
		private function disableLetGoButton():void
		{
			letGoButton.disable();
			letGoButton.removeEventListener(MouseEvent.CLICK, onLetGoButtonClick);
		}
		
		private function enableLetGoButton():void
		{
			letGoButton.enable();
			letGoButton.addEventListener(MouseEvent.CLICK, onLetGoButtonClick);
		}
		
		private function trimArray(arr:Array, length:int):void
		{
			var origLength:int = arr.length;
			for (var i:int = 0; i < origLength - length; i++)
			{
				var randomIndex:int = Math.random() * arr.length;
				arr[randomIndex] = arr[arr.length - 1];
				arr.pop();
			} 
		}
		
		//下一个嫌疑犯
		private function getNextSuspect():void
		{
			var clues:Array = new Array();
			for (var i:int = 0; i < battleCase.clues.length; i++)
			{
				clues.push(battleCase.clues[i]);
			}
			var clueLength:int = Math.random() * (clues.length - 1) + 1;
			trimArray(clues, clueLength);
			var magicStr:String;
			if (criminalIndex == currentSuspectIndex)
				magicStr = Profile.crimialStr;
			else
				magicStr = Profile.randomStr;
			ServerHelper.getInstance().getRandomSuspect(battleCase.caseId, clues, magicStr, getNextSuspectComplete);
		}
		
		//从已有的动物中获得随机嫌疑犯
		private function getNextSuspectFromNeighbor():int
		{
			var supectId:int = detectiveAnimalId;
			for (var animalIdStr:String in PlayerInfoHelper.getInstance().animals)
			{
				var animalId:int = parseInt(animalIdStr);
				if (animalId > 0 && animalId != detectiveAnimalId)
				{
					supectId = animalId;
					break;
				}
			}
			return supectId;
		}
	
		private function getNextSuspectComplete(animalId:int):void
		{
			if (suspectStage != null)
			{
				suspectStage.leftAnimal.stopTimer();
				removeChild(suspectStage);
			}
			//如果从数据库得到的随机嫌疑犯正好是侦探动物本人
			if (animalId == detectiveAnimalId)
				animalId = getNextSuspectFromNeighbor();
			var animal:AnimalInfo = PlayerInfoHelper.getInstance().animals[animalId];
			currentSuspectIndex++;
			suspectStage = new SuspectStage(animal);
			suspectStage.x = 440;
			suspectStage.y = BFont.stageHeight - 70;
			addChild(suspectStage);
			//显示嫌疑犯标题（主人,名字, 第几个嫌疑犯）
			titleText1.text = "第 " + currentSuspectIndex.toString() + " 个嫌疑犯\n";
			titleText1.x = 445 - titleText1.width / 2;
			titleText1.y = 70;
			titleText2.text = animal.ownerName + "的" + animal.animalName;
			titleText2.setTextFormat(new TextFormat("微软雅黑", 25, BFont.userNameColor), 0, animal.ownerName.length);
			titleText2.setTextFormat(new TextFormat("微软雅黑", 25, 0x000000), animal.ownerName.length, animal.ownerName.length + 1);
			titleText2.setTextFormat(new TextFormat("微软雅黑", 25, BFont.animalNameColor), animal.ownerName.length + 1, titleText2.text.length);
			titleText2.x = 445 - titleText2.width / 2;
			titleText2.y = 110;
						
			if (currentSuspectIndex < suspectTotalNum)
			{
				suspectLeftNumTxt.text = "后面还有 " + (suspectTotalNum - currentSuspectIndex).toString() + " 个";
				enableLetGoButton();
			}
			else
			{
				suspectLeftNumTxt.text = "已经是最后一个";
				disableLetGoButton();
			}
		}
		
		private function arrestButtonOnMouseOver(event:MouseEvent):void
		{
			if (null == arrestButtonTip)
			{
				var textStr:String = "错误指认是要赔偿骨头的！";
				arrestButtonTip = new BasicMessage(190, 30, textStr, 0x50AF50, 0x000000, true);
				arrestButtonTip.y = arrestButton.y - arrestButtonTip.height;
				arrestButtonTip.x = arrestButton.x + (arrestButton.width - arrestButtonTip.width) / 2;
				addChild(arrestButtonTip);
			}
		}
		
		private function arrestButtonOnMouseOut(event:MouseEvent):void
		{
			if(null != arrestButtonTip && null != arrestButtonTip.parent)
			{
				arrestButtonTip.parent.removeChild(arrestButtonTip);
				arrestButtonTip = null;
			}
		}
		
		private function letGoButtonOnMouseOver(event:MouseEvent):void
		{
			if (null == letGoButtonTip)
			{
				var textStr:String = "放走之后就不能回头了！";
				letGoButtonTip = new BasicMessage(190, 30, textStr, 0x50AF50, 0x000000, true);
				letGoButtonTip.y = letGoButton.y - letGoButtonTip.height;
				letGoButtonTip.x = letGoButton.x + (letGoButton.width - letGoButtonTip.width) / 2;
				addChild(letGoButtonTip);
			}
		}
		
		private function letGoButtonOnMouseOut(event:MouseEvent):void
		{
			if(null != letGoButtonTip && null != letGoButtonTip.parent)
			{
				letGoButtonTip.parent.removeChild(letGoButtonTip);
				letGoButtonTip = null;
			}
		}
		
		private function spiderOnMouseOver(event:MouseEvent):void
		{
			if (null == spiderTip)
			{
				spiderTip = new BasicMessage(150, 30, "别来烦我！", 0x50AF50, 0x000000);
				spiderTip.y = 100;
				spiderTip.x = 200;
				addChild(spiderTip);
			}
		}
		
		private function spiderOnMouseOut(event:MouseEvent):void
		{
			if(null != spiderTip && null != spiderTip.parent)
			{
				spiderTip.parent.removeChild(spiderTip);
				spiderTip = null;
			}
		}
		
	}
}